﻿$(function () {

    //===== Custom single file input =====//

    $("input.fileInput").filestyle({
        imageheight: 26,
        imagewidth: 89,
        width: 300
    });


    //===== Dual select boxes =====//

    $.configureBoxes();


    //===== Time picker =====//

    $('.timepicker').timeEntry({
        show24Hours: true, // 24 hours format
        showSeconds: true, // Show seconds?
        spinnerImage: 'images/ui/spinnerUpDown.png', // Arrows image
        spinnerSize: [17, 28, 0], // Image size
        spinnerIncDecOnly: true // Only up and down arrows
    });


    //===== Wizard =====//

    $('.wizard').smartWizard({
        selected: 0,  // Selected Step, 0 = first step   
        keyNavigation: true, // Enable/Disable key navigation(left and right keys are used if enabled)
        enableAllSteps: false,  // Enable/Disable all steps on first load
        transitionEffect: 'slideleft', // Effect on navigation, none/fade/slide/slideleft
        contentURL: null, // specifying content url enables ajax content loading
        contentCache: true, // cache step contents, if false content is fetched always from ajax url
        cycleSteps: false, // cycle step navigation
        enableFinishButton: false, // makes finish button enabled always
        errorSteps: [],    // array of step numbers to highlighting as error steps
        labelNext: 'Next', // label for Next button
        labelPrevious: 'Previous', // label for Previous button
        labelFinish: 'Finish',  // label for Finish button        
        // Events
        onLeaveStep: null, // triggers when leaving a step
        onShowStep: null,  // triggers when showing a step
        onFinish: null  // triggers when Finish button is clicked
    });

    //===== File uploader =====//

    $("#uploader").pluploadQueue({
        runtimes: 'html5,html4',
        url: 'php/upload.php',
        max_file_size: '2mb',
        unique_names: true,
        filters: [
			{ title: "Image files", extensions: "jpg,gif,png" },
			{ title: "Zip files", extensions: "zip" }
		]
    });



    //===== File manager =====//	
    //#region
    /*
    $('#fileManager').elfinder({
    url : 'php/connector.php',
    })
    */
    //#endregion

    //===== Alert windows =====//

    $(".bAlert").click(function () {
        jAlert('This is a custom alert box. Title and this text can be easily editted', 'Alert Dialog Sample');
    });

    $(".bConfirm").click(function () {
        jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
            jAlert('Confirmed: ' + r, 'Confirmation Results');
        });
    });

    $(".bPromt").click(function () {
        jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function (r) {
            if (r) alert('You entered ' + r);
        });
    });

    $(".bHtml").click(function () {
        jAlert('You can use HTML, such as <strong>bold</strong>, <em>italics</em>, and <u>underline</u>!');
    });


    //===== Accordion =====//		

    $('div.menu_body:eq(0)').show();
    $('.acc .head:eq(0)').show().css({ color: "#abb3ba" });

    $(".acc .head").click(function () {
        $(this).css({ color: "#abb3ba" }).next("div.menu_body").slideToggle(300).siblings("div.menu_body").slideUp("slow");
        $(this).siblings().css({ color: "#d5d5d5" });
    });


    //===== WYSIWYG editor =====//
    //#region
    /*
    $('.wysiwyg').wysiwyg({
    iFrameClass: "wysiwyg-input",
    controls: {
    bold          : { visible : true },
    italic        : { visible : true },
    underline     : { visible : true },
    strikeThrough : { visible : false },
			
    justifyLeft   : { visible : true },
    justifyCenter : { visible : true },
    justifyRight  : { visible : true },
    justifyFull   : { visible : true },
			
    indent  : { visible : true },
    outdent : { visible : true },
			
    subscript   : { visible : false },
    superscript : { visible : false },
			
    undo : { visible : true },
    redo : { visible : true },
			
    insertOrderedList    : { visible : true },
    insertUnorderedList  : { visible : true },
    insertHorizontalRule : { visible : false },

    h1: {
    visible: true,
    className: 'h1',
    command: ($.browser.msie || $.browser.safari) ? 'formatBlock' : 'heading',
    arguments: ($.browser.msie || $.browser.safari) ? '<h1>' : 'h1',
    tags: ['h1'],
    tooltip: 'Header 1'
    },
    h2: {
    visible: true,
    className: 'h2',
    command: ($.browser.msie || $.browser.safari) ? 'formatBlock' : 'heading',
    arguments: ($.browser.msie || $.browser.safari) ? '<h2>' : 'h2',
    tags: ['h2'],
    tooltip: 'Header 2'
    },
    h3: {
    visible: true,
    className: 'h3',
    command: ($.browser.msie || $.browser.safari) ? 'formatBlock' : 'heading',
    arguments: ($.browser.msie || $.browser.safari) ? '<h3>' : 'h3',
    tags: ['h3'],
    tooltip: 'Header 3'
    },
    h4: {
    visible: true,
    className: 'h4',
    command: ($.browser.msie || $.browser.safari) ? 'formatBlock' : 'heading',
    arguments: ($.browser.msie || $.browser.safari) ? '<h4>' : 'h4',
    tags: ['h4'],
    tooltip: 'Header 4'
    },
    h5: {
    visible: true,
    className: 'h5',
    command: ($.browser.msie || $.browser.safari) ? 'formatBlock' : 'heading',
    arguments: ($.browser.msie || $.browser.safari) ? '<h5>' : 'h5',
    tags: ['h5'],
    tooltip: 'Header 5'
    },
    h6: {
    visible: true,
    className: 'h6',
    command: ($.browser.msie || $.browser.safari) ? 'formatBlock' : 'heading',
    arguments: ($.browser.msie || $.browser.safari) ? '<h6>' : 'h6',
    tags: ['h6'],
    tooltip: 'Header 6'
    },
			
    cut   : { visible : true },
    copy  : { visible : true },
    paste : { visible : true },
    html  : { visible: true },
    increaseFontSize : { visible : false },
    decreaseFontSize : { visible : false },
    },
    events: {
    click: function(event) {
    if ($("#click-inform:checked").length > 0) {
    event.preventDefault();
    alert("You have clicked jWysiwyg content!");
    }
    }
    }
    });*/

    //$('.wysiwyg').wysiwyg("insertHtml", "Sample code");
    //#endregion

    //===== ToTop =====//

    $().UItoTop({ easingType: 'easeOutQuart' });


    //===== Spinner options =====//

    var itemList = [
		{ url: "http://ejohn.org", title: "John Resig" },
		{ url: "http://bassistance.de/", title: "J&ouml;rn Zaefferer" },
		{ url: "http://snook.ca/jonathan/", title: "Jonathan Snook" },
		{ url: "http://rdworth.org/", title: "Richard Worth" },
		{ url: "http://www.paulbakaus.com/", title: "Paul Bakaus" },
		{ url: "http://www.yehudakatz.com/", title: "Yehuda Katz" },
		{ url: "http://www.azarask.in/", title: "Aza Raskin" },
		{ url: "http://www.karlswedberg.com/", title: "Karl Swedberg" },
		{ url: "http://scottjehl.com/", title: "Scott Jehl" },
		{ url: "http://jdsharp.us/", title: "Jonathan Sharp" },
		{ url: "http://www.kevinhoyt.org/", title: "Kevin Hoyt" },
		{ url: "http://www.codylindley.com/", title: "Cody Lindley" },
		{ url: "http://malsup.com/jquery/", title: "Mike Alsup" }
	];

    var opts = {
        's1': { decimals: 2 },
        's2': { stepping: 0.25 },
        's3': { currency: '$' },
        's4': {},
        's5': {
            //
            // Two methods of adding external items to the spinner
            //
            // method 1: on initalisation call the add method directly and format html manually
            init: function (e, ui) {
                for (var i = 0; i < itemList.length; i++) {
                    ui.add('<a href="' + itemList[i].url + '" target="_blank">' + itemList[i].title + '</a>');
                }
            },

            // method 2: use the format and items options in combination
            format: '<a href="%(url)" target="_blank">%(title)</a>',
            items: itemList
        }
    };

    for (var n in opts)
        $("#" + n).spinner(opts[n]);

    $("button").click(function (e) {
        var ns = $(this).attr('id').match(/(s\d)\-(\w+)$/);
        if (ns != null)
            $('#' + ns[1]).spinner((ns[2] == 'create') ? opts[ns[1]] : ns[2]);
    });
    

    //===== ShowCode plugin for <pre> tag =====//

    $('.showCode').sourcerer('js html css php'); // Display all languages
    $('.showCodeJS').sourcerer('js'); // Display JS only
    $('.showCodeHTML').sourcerer('html'); // Display HTML only
    $('.showCodePHP').sourcerer('php'); // Display PHP only
    $('.showCodeCSS').sourcerer('css'); // Display CSS only


    //===== Calendar =====//

    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        editable: true,
        events: [
			{
			    title: 'All day event',
			    start: new Date(y, m, 1)
			},
			{
			    title: 'Long event',
			    start: new Date(y, m, 5),
			    end: new Date(y, m, 8)
			},
			{
			    id: 999,
			    title: 'Repeating event',
			    start: new Date(y, m, 2, 16, 0),
			    end: new Date(y, m, 3, 18, 0),
			    allDay: false
			},
			{
			    id: 999,
			    title: 'Repeating event',
			    start: new Date(y, m, 9, 16, 0),
			    end: new Date(y, m, 10, 18, 0),
			    allDay: false
			},
			{
			    title: 'Actually any color could be applied for background',
			    start: new Date(y, m, 30, 10, 30),
			    end: new Date(y, m, d + 1, 14, 0),
			    allDay: false,
			    color: '#B55D5C'
			},
			{
			    title: 'Lunch',
			    start: new Date(y, m, 14, 12, 0),
			    end: new Date(y, m, 15, 14, 0),
			    allDay: false
			},
			{
			    title: 'Birthday PARTY',
			    start: new Date(y, m, 18),
			    end: new Date(y, m, 20),
			    allDay: false
			},
			{
			    title: 'Click for Google',
			    start: new Date(y, m, 27),
			    end: new Date(y, m, 29),
			    url: 'http://google.com/'
			}
		]
    });


    //===== Dynamic data table =====//

    oTable = $('#example').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "sDom": '<""f>t<"F"lp>'
    });


    //===== Form elements styling =====//

    $('form').jqTransform({ imgPath: '../images/forms' });


    //===== Form validation engine =====//

    $("#valid").validationEngine();


    //===== Datepickers =====//

    $(".datepicker").datepicker({
        defaultDate: +7,
        autoSize: true,
        appendText: '(dd-mm-yyyy)',
        dateFormat: 'dd-mm-yy'
    });

    $(".datepickerInline").datepicker({
        defaultDate: +7,
        autoSize: true,
        appendText: '(dd-mm-yyyy)',
        dateFormat: 'dd-mm-yy',
        numberOfMonths: 1
    });


    //===== Progressbar (Jquery UI) =====//

    $("#progressbar").progressbar({
        value: 37
    });


    //===== Tooltip =====//

    $('.leftDir').tipsy({ fade: true, gravity: 'e' });
    $('.rightDir').tipsy({ fade: true, gravity: 'w' });
    $('.topDir').tipsy({ fade: true, gravity: 's' });
    $('.botDir').tipsy({ fade: true, gravity: 'n' });


    //===== Information boxes =====//

    $(".hideit").click(function () {
        $(this).fadeOut(400);
    });


    //=====Resizable table columns =====//

    var onSampleResized = function (e) {
        var columns = $(e.currentTarget).find("th");
        var msg = "columns widths: ";
        columns.each(function () { msg += $(this).width() + "px; "; })
    };

    $(".resize").colResizable({
        liveDrag: true,
        gripInnerHtml: "<div class='grip'></div>",
        draggingClass: "dragging",
        onResize: onSampleResized
    });


    //===== Left navigation submenu animation =====//	

    $("ul.sub li a").hover(function () {
        $(this).stop().animate({ color: "#a9a9a9" }, 400);
    }, function () {
        $(this).stop().animate({ color: "#d5d5d5" }, 400);
    });


    //===== Image gallery control buttons =====//
    /*
    $(".pics ul li").hover(
		  function () { $(this).children(".actions").show("fade", 200); },
		  function () { $(this).children(".actions").hide("fade", 200); }
	 );
    */
    //===== Color picker =====//

    $('#colorpickerField').ColorPicker({
        onSubmit: function (hsb, hex, rgb, el) {
            $(el).val(hex);
            $(el).ColorPickerHide();
        },
        onBeforeShow: function () {
            $(this).ColorPickerSetColor(this.value);
        }
    })
	.bind('keyup', function () {
	    $(this).ColorPickerSetColor(this.value);
	});


    //===== Autogrowing textarea =====//

    $(".auto").autoGrow();


    //===== Autotabs. Inline data rows =====//

    $('.onlyNums input').autotab_magic().autotab_filter('numeric');
    $('.onlyText input').autotab_magic().autotab_filter('text');
    $('.onlyAlpha input').autotab_magic().autotab_filter('alpha');
    $('.onlyRegex input').autotab_magic().autotab_filter({ format: 'custom', pattern: '[^0-9\.]' });
    $('.allUpper input').autotab_magic().autotab_filter({ format: 'alphanumeric', uppercase: true });


    //===== jQuery UI sliders =====//	

    $(".uiSlider").slider();

    $(".uiSliderInc").slider({
        value: 100,
        min: 0,
        max: 500,
        step: 50,
        slide: function (event, ui) {
            $("#amount").val("$" + ui.value);
        }
    });
    $("#amount").val("$" + $(".uiSliderInc").slider("value"));

    $(".uiRangeSlider").slider({
        range: true,
        min: 0,
        max: 500,
        values: [75, 300],
        slide: function (event, ui) {
            $("#rangeAmount").val("$" + ui.values[0] + " - $" + ui.values[1]);
        }
    });
    $("#rangeAmount").val("$" + $(".uiRangeSlider").slider("values", 0) + " - $" + $(".uiRangeSlider").slider("values", 1));

    $(".uiMinRange").slider({
        range: "min",
        value: 37,
        min: 1,
        max: 700,
        slide: function (event, ui) {
            $("#minRangeAmount").val("$" + ui.value);
        }
    });
    $("#minRangeAmount").val("$" + $(".uiMinRange").slider("value"));

    $(".uiMaxRange").slider({
        range: "max",
        min: 1,
        max: 100,
        value: 20,
        slide: function (event, ui) {
            $("#maxRangeAmount").val(ui.value);
        }
    });
    $("#maxRangeAmount").val($(".uiMaxRange").slider("value"));



    $("#eq > span").each(function () {
        // read initial values from markup and remove that
        var value = parseInt($(this).text(), 10);
        $(this).empty().slider({
            value: value,
            range: "min",
            animate: true,
            orientation: "vertical"
        });
    });


    //===== Breadcrumbs =====//	

    $("#breadCrumb").jBreadCrumb();


    //===== Autofocus =====//	

    $('.autoF').focus();


    //===== Tabs =====//		
    //#region
    /*
    $.fn.simpleTabs = function(){ 	    
    //Default Action
    $(this).find(".tab_content").hide(); //Hide all content
    $(this).find("ul.tabs li:first").addClass("activeTab").show(); //Activate first tab
    $(this).find(".tab_content:first").show(); //Show first tab content
	
    //On Click Event
    $("ul.tabs li").click(function() {
    $(this).parent().parent().find("ul.tabs li").removeClass("activeTab"); //Remove any "active" class
    $(this).addClass("activeTab"); //Add "active" class to selected tab
    $(this).parent().parent().find(".tab_content").hide(); //Hide all tab content
    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
    $(activeTab).show(); //Fade in the active content
    return false;
    });
	    
    };//end function
    
    $("div[class^='widget']").simpleTabs(); //Run function on any div with class name of "Simple Tabs"
    */
    //#endregion

    //===== Placeholder for all browsers =====//

    $("input").each(
		function () {
		    if ($(this).val() == "" && $(this).attr("placeholder") != "") {
		        $(this).val($(this).attr("placeholder"));
		        $(this).focus(function () {
		            if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
		        });
		        $(this).blur(function () {
		            if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
		        });
		    }
		});


    //===== User nav dropdown =====//		

    $('.dd').click(function () {
        $('ul.menu_body').slideToggle(100);
    });

    $('.acts').click(function () {
        $('ul.actsBody').slideToggle(100);
    });

    //===== Contacts list =====//

    $('#myList').listnav({
        /*initLetter: 'a', */
        includeAll: true,
        includeOther: true,
        flagDisabled: true,
        noMatchText: 'Không có dữ liệu. Vui lòng chọn ký tự khác.',
        prefixes: ['the', 'a']
    });

});